﻿import os;
import sys;
import xml.dom.minidom;
#import output;
import codecs;
import subprocess;
import binascii;
import shutil;
import stat;


SOURCE_FILE_PATH = "."
SOURCE_FILE_NAME = ["totalproto.xml","itemproto.xml","questproto.xml","attrproto.xml","buffproto.xml","otherinfoproto.xml","taskproto.xml","skillproto.xml","petproto.xml"]
#有新增的xml请修改上面的路径列表

LINE_HEAD = b'/* creation time:';
def fixFile( filename ):
    lines = open( filename, 'rb' ).read().split( b'\n' );
    os.chmod(filename, stat.S_IWRITE);
    f = open( filename, 'wb' );
    for line in lines:
        if line[0:len(LINE_HEAD)] == LINE_HEAD:
            #print( line );
            pass;
        else:
            f.write(line);
            f.write(b'\n');
    f.close();

gUpdateFiles = 0;
gSkipFiles = 0;
gNewFiles = 0;
def updateFiles( src_dir, dst_dir ):
    global gUpdateFiles;
    global gSkipFiles;
    global gNewFiles;
    for fname in os.listdir( src_dir):
        src = os.path.join(src_dir,fname);
        dst = os.path.join(dst_dir,fname);
        if os.path.isdir(src):
            if not os.path.isdir(dst):
                os.mkdir(dst);
            updateFiles(src,dst);
        elif os.path.isfile(src):
            os.chmod(src, stat.S_IWRITE);
            fixFile(src);
            if os.path.isfile(dst):
                os.chmod(dst, stat.S_IWRITE);
                src_crc = binascii.crc32( open( src, 'rb' ).read() );
                dst_crc = binascii.crc32( open( dst, 'rb' ).read() );
                if src_crc != dst_crc:
                    print( "update", dst );
                    shutil.copyfile(src, dst)
                    gUpdateFiles += 1;
                else:
                    #print( "skip", dst );
                    gSkipFiles += 1;
                    pass;
            else:
                print( "create", dst );
                shutil.copyfile(src, dst)
                gNewFiles += 1;

        
def TdrPaketConvert():
    sum_filename= ' '.join(SOURCE_FILE_NAME);
    TMP_DIR = "tmp";
    DST_DIR = "../PacketCode";
    if os.path.isdir(TMP_DIR):
        shutil.rmtree(TMP_DIR);
    os.mkdir(TMP_DIR);
    buildstr = '"tdr.exe " -P -p -O ' + TMP_DIR + ' ' + sum_filename;
    print(buildstr);
    nResult = os.system(buildstr);
    if nResult != 0:
        print ("build error!");
        sys.exit(1);
        return;
    shutil.copy("TdrBufBack.h",TMP_DIR+"/TdrBuf.h")
    shutil.copy("TdrBufBack.cpp",TMP_DIR+"/TdrBuf.cpp")
    updateFiles(TMP_DIR,DST_DIR);
    global gUpdateFiles;
    global gSkipFiles;
    global gNewFiles;
    print( "%d file(s) skipped" % gSkipFiles );
    print( "%d file(s) created" % gNewFiles );
    print( "%d file(s) updated" % gUpdateFiles );





if __name__ == "__main__":
    if len( sys.argv ) >= 2:
        SOURCE_FILE_PATH = os.path.abspath(sys.argv[1]);
        os.chdir(SOURCE_FILE_PATH);
    TdrPaketConvert();
    if len( sys.argv ) < 2:
        os.system("pause");
    sys.exit(0);
